//
//  ByBasketBallMixedPopView.m
//  caipiao_ios
//
//  Created by mc on 2018/8/20.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBasketBallMixedPopView.h"
#import "BySureCancelView.h"

#define ViewWidth  (SCREEN_WIDTH - 20)
#define WIDTH (ViewWidth - 20)
@interface ByBasketBallMixedPopView ()


@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIScrollView *scrollView;


@property(nonatomic,strong)UIView * sfView;


@property(nonatomic,strong)UIView * bigSmallView;

@property(nonatomic,strong)UIView * sfcView;

@property(nonatomic,strong) NSMutableArray * sfArray;
@property(nonatomic,strong) NSMutableArray * rsfArray;
@property(nonatomic,strong) NSMutableArray * bigSmallArray;
@property(nonatomic,strong) NSMutableArray * sfcArray;

@property(nonatomic,strong) NSMutableArray * sfArray1;
@property(nonatomic,strong) NSMutableArray * rsfArray1;
@property(nonatomic,strong) NSMutableArray * bigSmallArray1;
@property(nonatomic,strong) NSMutableArray * sfcArray1;


@end


@implementation ByBasketBallMixedPopView

- (id)init{
    self = [super init];
    if (self) {
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backgroundView.backgroundColor = rgba(120, 120, 120, 0.3);
        self.backgroundView = backgroundView;
        [[UIApplication sharedApplication].keyWindow addSubview:backgroundView];
        
        
        CGFloat height = 50 + 30 + 93 + 68 + 103 + 60;
        self.frame = CGRectMake(10, (SCREEN_HEIGHT - height) / 2.0, ViewWidth,height);
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 5;
        self.backgroundColor = BY_BACKGROUND_COLOR;
        
        [self initTitleViews];
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, ViewWidth, self.frame.size.height - 50 - 60)];
        [self addSubview:self.scrollView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, 30)];
        [self.scrollView addSubview:titleLabel];
        titleLabel.text = @"仅胜负差可投单关";
        titleLabel.font = FONT(16);
        titleLabel.textColor = BY_THEME_COLOR;;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        self.scrollView.contentSize = CGSizeMake(ViewWidth, self.scrollView.size.height);
        
        
        self.sfView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, WIDTH, 73)];
        [self.scrollView addSubview:self.sfView];
        self.sfView.backgroundColor = BY_LINE_COLOR;
        self.sfView.layer.cornerRadius = 5;
        self.sfView.layer.masksToBounds = YES;
        
        
        
        self.bigSmallView = [[UIView alloc] initWithFrame:CGRectMake(10, self.sfView.bottom + 20, WIDTH, 45)];
        [self.scrollView addSubview:self.bigSmallView];
        self.bigSmallView.backgroundColor = BY_LINE_COLOR;
        self.bigSmallView.layer.cornerRadius = 5;
        self.bigSmallView.layer.masksToBounds = YES;
        
        
        
        
        self.sfcView = [[UIView alloc] initWithFrame:CGRectMake(10, self.bigSmallView.bottom + 20, WIDTH, 73)];
        [self.scrollView addSubview:self.sfcView];
        self.sfcView.backgroundColor = BY_LINE_COLOR;
        self.sfcView.layer.cornerRadius = 5;
        self.sfcView.layer.masksToBounds = YES;
        
        
        
        BySureCancelView *sureCancelView = [[BySureCancelView alloc] init];
        [self addSubview:sureCancelView];
        [sureCancelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(0);
            make.right.equalTo(self).offset(0);
            make.bottom.equalTo(self).offset(0);
            make.height.mas_equalTo(@60);
        }];
        sureCancelView.cancelBlock = ^(){
            [self removeViews];
            self.selectedBlock(self.sfArray1 ,self.rsfArray1, self.bigSmallArray1 , self.sfcArray1);
          
        };
        sureCancelView.sureBlock = ^(){
            
            [self removeViews];
            
            if (self.selectedBlock) {
                self.selectedBlock(self.sfArray ,self.rsfArray, self.bigSmallArray , self.sfcArray);
            }
        };
       
        
        [[UIApplication sharedApplication].keyWindow addSubview:self];
    }
    return self;
}

- (void)setDataSfArray:(NSMutableArray *)sfArray AndRsfArray:(NSMutableArray *)rsfArray AndBigSmallArray:(NSMutableArray *)bigSmallArray AndSfcArray:(NSMutableArray *)sfcArray{
    
    
    self.sfArray1 = [NSMutableArray arrayWithCapacity:1];
    self.rsfArray1 = [NSMutableArray arrayWithCapacity:1];
    self.bigSmallArray1 = [NSMutableArray arrayWithCapacity:1];
    self.sfcArray1 = [NSMutableArray arrayWithCapacity:1];
    
    ByNetModel *model = [sfArray firstObject];
    self.bottomView.isBasketBall = YES;
    self.bottomView.netModel = model;
    
    for (ByNetModel *netModel in sfArray) {
        ByNetModel *model = [netModel copy];
        [self.sfArray1 addObject:model];
    }
    
    for (ByNetModel *netModel in rsfArray) {
        ByNetModel *model = [netModel copy];
        [self.rsfArray1 addObject:model];
    }
    
    for (ByNetModel *netModel in bigSmallArray) {
        ByNetModel *model = [netModel copy];
        [self.bigSmallArray1 addObject:model];
    }
    
    for (ByNetModel *netModel in sfcArray) {
        ByNetModel *model = [netModel copy];
        [self.sfcArray1 addObject:model];
    }
    
    self.sfArray = sfArray;
    self.rsfArray = rsfArray;
    self.bigSmallArray = bigSmallArray;
    self.sfcArray = sfcArray;
    
    [self initSfViews];
    [self initBigSmallViews];
    [self initSfcViews];
}

- (void)initTitleViews{
    ByTootBallPopTitleView *bottomView = [[ByTootBallPopTitleView alloc] init];
    [self addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.top.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.height.mas_equalTo(@50);;
    }];
    self.bottomView = bottomView ;
}

- (void)initSfViews{

    [self.sfView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 35) backgroundColor:rgba(116, 180, 226, 1) text:@"非\n让\n分" textColor:[UIColor whiteColor] font:9];
    [self.sfView addSubview:pointLabel1];
    
    UILabel *pointLabel2 = [self createLabelWithFrame:CGRectMake(1, 36, 20, 35) backgroundColor:BY_THEME_COLOR text:@"让\n分" textColor:[UIColor whiteColor] font:9];
    [self.sfView addSubview:pointLabel2];
    pointLabel1.numberOfLines = 0;
    pointLabel2.numberOfLines = 0;
    
    
    CGFloat left = 1;
    CGFloat magin = 1;
    CGFloat width = (WIDTH - 21 - left * 3) / 2.0;
    CGFloat height = 35;
    
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:1];
    [dataArray addObjectsFromArray:[self.sfArray copy]];
    [dataArray addObjectsFromArray:[self.rsfArray copy]];
    ByNetModel *secondModel = [dataArray objectAtIndex:2];
    pointLabel2.text = secondModel.fixedodds;
    NSLog(@"2222222     %@",secondModel.fixedodds);
    
    for (int i = 0;i < 4; i++) {
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(22 + i % 2 * (width + magin),1 + i / 2 * (height + magin), width,height)];
        [self.sfView addSubview:valueLabel];
        valueLabel.tag = 100 + i;
        valueLabel.font = FONT(14);
        valueLabel.textAlignment = NSTextAlignmentCenter;
        ByNetModel *netModel = [dataArray objectAtIndex:i];
        if (netModel.selected) {
            valueLabel.backgroundColor = BY_THEME_COLOR;
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
            
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(spfTapAction:)];
        [valueLabel addGestureRecognizer:tap];
        if (netModel.isNoSale) {
            valueLabel.userInteractionEnabled = NO;
            valueLabel.text = @"未开售";
        }else{
            valueLabel.userInteractionEnabled = YES;
           valueLabel.text = [NSString stringWithFormat:@"%@ %@", netModel.title,netModel.valueString];
        }
      
    }
}

- (void)spfTapAction:(UITapGestureRecognizer *)tap{
    NSInteger index = tap.view.tag - 100;
    if (index < 2) {
        ByNetModel *netModel = [self.sfArray objectAtIndex:index];
        netModel.selected = !netModel.selected;
    }else{
       
        ByNetModel *netModel = [self.rsfArray objectAtIndex:index - 2];
        netModel.selected = !netModel.selected;
    }
    [self initSfViews];
}


- (void)initBigSmallViews{
    [self.bigSmallView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 45) backgroundColor:rgba(217, 184, 29, 1) text:@"大\n小\n分" textColor:[UIColor whiteColor] font:9];
    [self.bigSmallView addSubview:pointLabel1];
    pointLabel1.numberOfLines = 0;
    
    UIView *backgroundView = [[UIView alloc] init];
    [self.bigSmallView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bigSmallView).offset(0);
        make.left.equalTo(pointLabel1.mas_right).offset(1);
        make.right.equalTo(self.bigSmallView).offset(0);
        make.bottom.equalTo(self.bigSmallView).offset(0);
    }];
    UILabel *vsLabel = [[UILabel alloc] init];
    [backgroundView addSubview:vsLabel];
    [vsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backgroundView).offset(0);
        make.centerY.equalTo(backgroundView).offset(0);
        make.size.mas_equalTo(CGSizeMake(50, 20));
    }];
    
    vsLabel.textColor = [UIColor blackColor];
    vsLabel.textAlignment = NSTextAlignmentCenter;
    vsLabel.font = FONT(14);
    
    
    ByNetModel *fistModel = [self.bigSmallArray firstObject];
    ByNetModel *lastModel = [self.bigSmallArray lastObject];
    
    vsLabel.text = fistModel.fixedodds;
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.centerY.equalTo(vsLabel.mas_centerY).offset(0);
        make.right.equalTo(vsLabel.mas_left).offset(0);
        make.height.mas_equalTo(@45);
    }];
    leftBtn.tag = 100;
    [leftBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.layer.borderColor = BY_LINE_COLOR.CGColor;
    leftBtn.layer.borderWidth = 1;
    [leftBtn setTitle:[NSString stringWithFormat:@"%@ %@", fistModel.title,fistModel.valueString] forState:UIControlStateNormal];
    if (fistModel.selected) {
        leftBtn.backgroundColor = BY_THEME_COLOR;
        [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       
    }else{
        leftBtn.backgroundColor = [UIColor whiteColor];
        [leftBtn setTitleColor:[UIColor  lightGrayColor] forState:UIControlStateNormal];
    }
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundView addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(0);
        make.centerY.equalTo(vsLabel.mas_centerY).offset(0);
        make.left.equalTo(vsLabel.mas_right).offset(0);
        make.height.mas_equalTo(@45);
    }];
    rightBtn.tag = 200;
    [rightBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.layer.borderColor = BY_LINE_COLOR.CGColor;
    rightBtn.layer.borderWidth = 1;
     [rightBtn setTitle:[NSString stringWithFormat:@"%@ %@", lastModel.title,lastModel.valueString] forState:UIControlStateNormal];
    if (lastModel.selected) {
        rightBtn.backgroundColor = BY_THEME_COLOR;
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        
        rightBtn.backgroundColor = [UIColor whiteColor];
        [rightBtn setTitleColor:[UIColor  lightGrayColor] forState:UIControlStateNormal];
       
    }
    
}

- (void)btnAction:(UIButton *)btn{
   
    if (btn.tag == 100) {
        ByNetModel *netModel = [self.bigSmallArray firstObject];
        netModel.selected = !netModel.selected;
    }else{
        ByNetModel *netModel = [self.bigSmallArray lastObject];
        netModel.selected = !netModel.selected;
    }
    
    [self initBigSmallViews];
}

- (void)initSfcViews{
    [self.sfcView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UILabel *pointLabel1 = [self createLabelWithFrame:CGRectMake(1, 1, 20, 35) backgroundColor:[UIColor colorWithHexString:@"#42B550"] text:@"主\n胜" textColor:[UIColor whiteColor] font:9];
    [self.sfcView addSubview:pointLabel1];
    
    UILabel *pointLabel2 = [self createLabelWithFrame:CGRectMake(1, 36, 20, 35) backgroundColor:[UIColor colorWithHexString:@"#BA68C9"] text:@"客\n胜" textColor:[UIColor whiteColor] font:9];
    [self.sfcView addSubview:pointLabel2];
    pointLabel1.numberOfLines = 0;
    pointLabel2.numberOfLines = 0;
    
    
    CGFloat left = 1;
    CGFloat magin = 1;
    CGFloat width = (WIDTH - 21 - left * 7) / 6.0;
    CGFloat height = 35;
    
    for (int i = 0;i < self.sfcArray.count; i++){
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(21 + i % 6 * (width + magin),1 + i / 6 * (height + magin), width,height)];
        [self.sfcView addSubview:valueLabel];
        valueLabel.tag = 100 + i;
        valueLabel.font = FONT(12);
        valueLabel.numberOfLines = 0;
        valueLabel.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [valueLabel addGestureRecognizer:tap];
        ByNetModel *netModel = [self.sfcArray objectAtIndex:i];
        if (netModel.isNoSale) {
            valueLabel.userInteractionEnabled = NO;
            valueLabel.text = @"未开售";
        }else{
            valueLabel.userInteractionEnabled = YES;
            valueLabel.text = [NSString stringWithFormat:@"%@\n%@", netModel.title,netModel.valueString];
        }
        if (netModel.selected) {
            valueLabel.backgroundColor = BY_THEME_COLOR;
            valueLabel.textColor = [UIColor whiteColor];
        }else{
            valueLabel.textColor = [UIColor lightGrayColor];
            valueLabel.backgroundColor = [UIColor whiteColor];
        }
    }
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    
    NSInteger index = tap.view.tag - 100;
    ByNetModel *netModel = [self.sfcArray objectAtIndex:index];
    netModel.selected = !netModel.selected;
    
    [self initSfcViews];
}


- (void)removeViews{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.origin.x-SCREEN_WIDTH, self.frame.origin.y,self.frame.size.width, self.frame.size.height);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.backgroundView removeFromSuperview];
        
        [self removeFromSuperview];
    });
}

- (UILabel *)createLabelWithFrame:(CGRect)frame backgroundColor:(UIColor *)backColor text:(NSString *)text textColor:(UIColor *)textColor font:(int)font{
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    if (backColor) {
        label.backgroundColor = backColor;
    }else{
        label.backgroundColor = [UIColor clearColor];
    }
    label.text = text;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

@end
